package com.cg.controller;



import com.cg.common.Result;
import com.cg.pojo.entity.Eventcategorie;
import com.cg.pojo.entity.UserEntity;
import com.cg.service.EventcategorieService;
import com.cg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eventcategorie")
@CrossOrigin

public class EventcategorieController {

    @Autowired
    private EventcategorieService eventcategorieService;

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public Result getAll(@RequestParam String username){
        UserEntity user = userService.getUserByUsername(username);
        List<Eventcategorie> list = eventcategorieService.getAll(user.getId());
        return Result.succsee(list);

    }

    @RequestMapping("/add")
    public Result<String> add(@RequestBody Eventcategorie eventcategorie){
        System.out.println(eventcategorie);
        eventcategorieService.add(eventcategorie);
        return Result.succsee("添加成功");
    }

    @RequestMapping("/update")
    public Result<String> update(@RequestBody Eventcategorie eventcategorie){
        eventcategorieService.update(eventcategorie);
        return Result.succsee("更新成功");

    }

    @RequestMapping("/delete")
    public Result<String> delete(@RequestBody Eventcategorie eventcategorie) {
        eventcategorieService.delete(eventcategorie.getCategoryId());
        return Result.succsee("删除成功");
    }
    @RequestMapping("/get/{categoryId}")
    public Result<Eventcategorie> get(@PathVariable("categoryId") Integer categoryId) {
        Eventcategorie eventcategorie = eventcategorieService.getById(categoryId);
        return Result.succsee(eventcategorie);
    }
    @RequestMapping("/getAll/{username}")
    public Result<List<Eventcategorie>> getAll1(@PathVariable String username){
        UserEntity user = userService.getUserByUsername(username);
        List<Eventcategorie> list = eventcategorieService.getAll(user.getId());
        return Result.succsee(list);
    }
}
